package com.xff.Controller;

import com.xff.dao.BookDao;
import com.xff.dao.TypeDao;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class BookTypeController {

    @PostMapping("/AddBookTypeServlet")
    public String AddBookTypeServlet(HttpServletRequest request, HttpSession session){
        if(session.getAttribute("status")!=null&&(int)session.getAttribute("status")==2) {
            //获取图书分类的名称
            String name = request.getParameter("name");
            TypeDao typedao = new TypeDao();
            //调用函数存入图书分类信息
            typedao.addBookType(name);
            return "redirect:/admin_booktype";
        }
        else {
            return "redirect:/login";
        }
    }

    @RequestMapping("/DeleteTypeServlet")
    public String DeleteTypeServlet(HttpServletRequest request,HttpSession session){
        if(session.getAttribute("status")!=null&&(int)session.getAttribute("status")==2) {
            int tid = Integer.parseInt(request.getParameter("tid"));
            TypeDao typedao = new TypeDao();
            typedao.deleteBookType(tid);
            return "redirect:/admin_booktype";
        }
        else {
            return "redirect:/login";
        }
    }

    @PostMapping("/UpdateBookTypeServlet")
    public  String UpdateBookTypeServlet(HttpServletRequest request,HttpSession session){
        if(session.getAttribute("status")!=null&&(int)session.getAttribute("status")==2) {
            String name = request.getParameter("name");
            String oldname = request.getParameter("oldname");
            //Integer 类：parseInt() 方法用于将字符串参数作为有符号的十进制整数进行解析
            //分类的id
            int tid = Integer.parseInt(request.getParameter("tid"));
            TypeDao typedao = new TypeDao();
            typedao.updateTypeBook(tid,name);
            //修改所有该类别的图书的分类
            new BookDao().updateBooks_type(oldname,name);
            return "redirect:/admin_booktype";
        }
        else {
            return "redirect:/login";
        }
    }
}
